package hu.elte.inf.holiuml.gui;

import hu.elte.inf.holiuml.model.Connection;
import hu.elte.inf.holiuml.model.ConnectionEdge;
import hu.elte.inf.holiuml.model.ObjectNode;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import java.util.Vector;

/**
 * @author Istvan Bozo
 */
public class ConnectionEdgeOptionsDialog extends OptionsDialog {

	private JPanel propPanel;
	JComboBox object1Combo;
	JComboBox object2Combo;
	JComboBox connTypeComboBox;
	JComboBox connNameComboBox;
	ObjectNode beginNode;
	ObjectNode endNode;
	ConnectionEdge connEdge;
	Vector<ObjectNode> objectNodes;
	private Vector<String> objectNames = new Vector<String>();

	public ConnectionEdgeOptionsDialog(HoliUML parent, ObjectNode beginNode, ObjectNode endNode,
			Vector<ObjectNode> objectNodes, ConnectionEdge connEdge) {
		super(parent, "Edge Options");
		this.beginNode = beginNode;
		this.endNode = endNode;
		this.objectNodes = new Vector<ObjectNode>(objectNodes);
		setNames();
		createPropPanel();
		fillPanels();
		setSize(new Dimension(450, 200));
		setModalityType(ModalityType.APPLICATION_MODAL);
		if(connEdge != null){
			this.connEdge = connEdge;
			connNameComboBox.addItem(connEdge.getConnection().getName());
		}
		setVisible(true);
	}
	
	private void setNames() {
		for(int i = 0; i < objectNodes.size(); i++){
			objectNames.add(objectNodes.get(i).getUMLObject().getName());
		}
	}

	private void setComboIndex(ObjectNode objectNode, JComboBox jComboBox){
		int index = objectNodes.indexOf(objectNode);
		jComboBox.setSelectedIndex(index);
	}
	
	private void createPropPanel() {
		propPanel = new JPanel(new GridLayout(4, 2));
		object1Combo = new JComboBox(objectNames);
		object1Combo.setEditable(false);
		setComboIndex(beginNode, object1Combo);
		object2Combo = new JComboBox(objectNames);
		object2Combo.setEditable(false);
		setComboIndex(endNode, object2Combo);
		// fill types
		Vector<String> types = new Vector<String>();
		types.add(new String("ASSOCIATION"));
		types.add(new String("COMPOSITION"));
		types.add(new String("AGGREGATION"));
		connTypeComboBox = new JComboBox(types);
		connNameComboBox = new JComboBox();
		connNameComboBox.setEditable(true);
		propPanel.add(new JLabel("Name of the first object:"));
		propPanel.add(object1Combo);
		propPanel.add(new JLabel("Name of the second object:"));
		propPanel.add(object2Combo);
		propPanel.add(new JLabel("Type of the connection:"));
		propPanel.add(connTypeComboBox);
		propPanel.add(new JLabel("Name of the connection:"));
		propPanel.add(connNameComboBox);

	}

	private void fillPanels() {
		getContentPane().setLayout(new BorderLayout());
		getContentPane().add("North", propPanel);
		getContentPane().add("South", buttonPanel);
	}

	public ConnectionEdge createConnection() {
		ObjectNode beginNode = objectNodes.get(object1Combo.getSelectedIndex());
		ObjectNode endNode = objectNodes.get(object2Combo.getSelectedIndex());
		Connection connection = new Connection(beginNode.getUMLObject(), endNode.getUMLObject());
		ConnectionEdge connectionEdge = new ConnectionEdge();
		connectionEdge.setConnection(connection);
		connectionEdge.setNodeBegin(beginNode);
		connectionEdge.setNodeEnd(endNode);
		connection.setName("dfsafs");
		return connectionEdge;
	}
	
	public void modifyConnection(ConnectionEdge connEdge) {
		connEdge.setNodeBegin(objectNodes.get(object1Combo.getSelectedIndex()));
		connEdge.setNodeEnd(objectNodes.get(object2Combo.getSelectedIndex()));
		connEdge.getConnection().setConnectionBegin(((ObjectNode)connEdge.getNodeBegin()).getUMLObject());
		connEdge.getConnection().setConnectionEnd(((ObjectNode)connEdge.getNodeEnd()).getUMLObject());
		connEdge.getConnection().setName(connNameComboBox.getSelectedItem().toString());
	}

	protected boolean processOK() {
		return true;
	}

	protected void processCancel() {
	}
}
